/*
 * Copyright 2008 Andrej Herich
 * 
 * http://azetclient.yw.sk/
 * http://azetclient.googlecode.com/
 * 
 * This file is part of AzetClient.
 * 
 * AzetClient is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * AzetClient is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with AzetClient.  If not, see <http://www.gnu.org/licenses/>.
 */

package sk.yw.azetclient.gui;

import java.awt.Component;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import org.apache.log4j.Logger;
import sk.yw.azetclient.Main;
import sk.yw.azetclient.model.Message;
import sk.yw.azetclient.model.MessageThread;

/**
 *
 * @author error216
 */
public class MessageThreadCellRenderer implements TableCellRenderer {
    
    private static final Logger logger = Logger.getLogger(
            MessageThreadCellRenderer.class);
    
    private int maxLengthOfMessage;
    
    public MessageThreadCellRenderer() {
        try {
            maxLengthOfMessage = Integer.parseInt(Main.getProperty(
                    "azetclient.conversationList.message.maxLength"));
        } catch (NumberFormatException ex) {
            logger.error("Invalid number format: " + Main.getProperty(
                    "azetclient.conversationList.message.maxLength"), ex);
            maxLengthOfMessage = 40;
        }
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        return getCellRendererComponent(table, value, isSelected, hasFocus);
    }
    
    public Component getCellRendererComponent(Component base, Object value,
            boolean isSelected, boolean hasFocus) {
        JLabel component = new JLabel();
        
        if (value == null) {
            component.setText("null");
        } else if (!(value instanceof MessageThread)) {
            component.setText(value.toString());
        } else {
            MessageThread thread = (MessageThread) value;
            component.setText(thread.getName());
            
            List<Message> messages = thread.getMessages();
            StringBuilder toolTipText = new StringBuilder();
            toolTipText.append("<html>");
            if (messages.size() > 1) {
                toolTipText.append(
                        messageToString(messages.get(messages.size() - 2)));
                toolTipText.append("<br>");
            }
            if (messages.size() > 0) {
                toolTipText.append(
                        messageToString(messages.get(messages.size() - 1)));
            }
            toolTipText.append("</html>");
            component.setToolTipText(toolTipText.toString());
        }
        
        component.setOpaque(true);
        if (base instanceof JTable) {
            JTable table = (JTable) base;
            if (isSelected) {
                component.setBackground(table.getSelectionBackground());
                component.setForeground(table.getSelectionForeground());
            } else {
                component.setBackground(table.getBackground());
                component.setForeground(table.getForeground());
            }
        }
        
        return component;
    }
    
    public String messageToString(Message message) {
        if (message.getContent().length() > maxLengthOfMessage) {
            return message.getContent()
                    .substring(0, maxLengthOfMessage) + "...";
        } else {
            return message.getContent();
        }
    }

}
